package com.per.mybootall.shiro;

import com.per.mybootall.common.Const;
import com.per.mybootall.dao.UserMapper;
import com.per.mybootall.pojo.User;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author 袁
 * 自定义的realm程序
 */

public class UserRealm extends AuthorizingRealm {

    @Autowired
    private UserMapper userMapper;
    /*执行授权逻辑*/
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("执行授权逻辑");
       // SimpleAuthenticationInfo info=new SimpleAuthenticationInfo();

        return null;
    }

    /*执行认证逻辑*/
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

        System.out.println("执行认证逻辑");

        UsernamePasswordToken token=(UsernamePasswordToken)authenticationToken;

      User user= userMapper.selectByUsernameforUser(((UsernamePasswordToken) authenticationToken).getUsername().toString());

        if (user==null){
            //用户名不存在，shiro底层会抛出一个UnknownAccountException异常
            System.out.println("313131313311");
            return null;
        }
        return new SimpleAuthenticationInfo(user,user.getPassword(),"");
    }
}
